package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.jiayen.util.DisplayUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.HeadLineDialogAdapter;
import com.jp.knowledge.adapter.OnItemListerner;
import com.jp.knowledge.model.HeadLine;
import com.jp.knowledge.util.L;

import org.xutils.common.util.DensityUtil;

import java.util.List;

/**
 * Created by jiayen on 17/6/1.
 * 导航dialog
 */

public class HeadLineDailog extends Dialog {
    private Context context;
    private View mView;
    private RecyclerView listView;
    private HeadLineDialogAdapter adapter;
    private List<HeadLine> list;

    private HeadDialogCallback callback;
    public HeadLineDailog(Context context,List<HeadLine> list) {
        this(context,R.style.custom_idalgo);
        this.list=list;
//        dialogWidth= DisplayUtil.dip2px(context,80);
//        topHeight = DisplayUtil.dip2px(context,50);
//        screenWidth = DensityUtil.getScreenWidth();
        adapter = new HeadLineDialogAdapter(context,list);
        this.context=context;
    }

    public HeadLineDailog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public void setCallback(HeadDialogCallback callback) {
        this.callback = callback;
    }

    /**
     * 更新
     * @param list
     */
    public void update(List<HeadLine> list){
        this.list=list;
        adapter.updataAdapter(list);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mView =  LayoutInflater.from(context).inflate(R.layout.head_line_dialog,null);//第一次先初始化
        listView = (RecyclerView) mView.findViewById(R.id.list_view);
        GridLayoutManager manager = new GridLayoutManager(context,3, LinearLayoutManager.VERTICAL,false);
        listView.setLayoutManager(manager);
        listView.setAdapter(adapter);
        setContentView(mView);
//        dialogWindow = getWindow(); //得到对话框
//        WindowManager.LayoutParams wl = dialogWindow.getAttributes();
//        wl.width=dialogWidth;
//        dialogWindow.setWindowAnimations(R.style.dialog_anim); //设置窗口弹出动画
        setCanceledOnTouchOutside(true);//点空白消失
        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width= DensityUtil.getScreenWidth()-30;
        lp.height=  WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

        adapter.setOnItemListener(new OnItemListerner() {
            @Override
            public void onItemClick(View view, int position) {
                if(callback!=null)
                    callback.done(list.get(position),position);
                dismiss();
            }
        });
    }
    public void showDialog(){

        show();
//        show(screenWidth/2,topHeight);
    }

//    public void show(int x, int y) {
//        L.e("show","======x="+x+"=======y="+y);
//        windowDeploy(x, y);
//        setCanceledOnTouchOutside(true);
//        show();
//    }
//
//    //设置窗口显示
//    private void windowDeploy(int x, int y) {
//        dialogWindow = getWindow(); //得到对话框
//        dialogWindow.setGravity(Gravity.LEFT|Gravity.TOP);
//
//        dialogWindow.setBackgroundDrawableResource(R.color.translate); //设置对话框背景为透明
//        WindowManager.LayoutParams wl = dialogWindow.getAttributes();
//        //根据x，y坐标设置窗口需要显示的位置
//
//        wl.x=x-dialogWidth/2;
//
//        wl.y = y; //y小于0上移，大于0下移
//        dialogWindow.setAttributes(wl);
//    }

    public interface   HeadDialogCallback{
        void done(HeadLine headLine,int position);
    }
}
